//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.kakarote.core.utils;

import cn.hutool.crypto.SecureUtil;
import com.kakarote.common.entity.UserInfo;
import com.kakarote.common.servlet.UserStrategy;

import java.util.Arrays;

public class UserUtil {
    private static UserStrategy userStrategy;

    public UserUtil() {
    }

    public static void setUserStrategy(UserStrategy userStrategy) {
        UserUtil.userStrategy = userStrategy;
    }

    public static String sign(String username, String password, String salt) {
        return SecureUtil.md5(username + password + "wukong".concat(salt));
    }

    public static boolean verify(String username, String password, String salt, String sign) {
        return sign.equals(sign(username, password, salt));
    }

    public static void setUser(UserInfo user) {
        if (userStrategy != null) {
            userStrategy.setUser(user);
        }

    }

    public static void setUser(Long userId) {
        if (userStrategy != null) {
            userStrategy.setUser(userId);
        }

    }

    public static UserInfo getUser() {
        UserInfo userInfo = new UserInfo();
        userInfo.setRoles(Arrays.asList(1559019597340197142L));//主管
        userInfo.setUserId(999999L);
        userInfo.setAdmin(true);
        userInfo.setUsername("管理员");
        return userInfo;

//        return userStrategy != null ? userStrategy.getUser() : null;
    }

    public static Long getUserId() {
        UserInfo user = getUser();
        return user != null ? user.getUserId() : null;
    }

    public static Long getPoolId() {
        UserInfo user = getUser();
        return user != null ? user.getPoolId() : null;
    }

    public static void removeUser() {
        if (userStrategy != null) {
            userStrategy.removeUser();
        }

    }

    public static boolean isAdmin() {
        UserInfo user = getUser();
        return user != null && user.isAdmin();
    }
}
